package com.whut.transportation.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.whut.transportation.R;
import com.whut.transportation.adapter.TransinfoAdapter.MyClickListener;
import com.whut.transportation.entity.OverWeightInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
/**
 * 超重数据绑定 适配器
 * @author
 */
public class OWinfoAdapter extends BaseAdapter {
	private List<OverWeightInfo> overWeightInfos;//要绑定的数据
	public static List<Boolean> mcheck;
	private int resource;//绑定的条目界面
	private LayoutInflater inflater;//布局填充器
	private MyClickListener mListener;
	
	private HashMap<Integer, View> mView = new HashMap<Integer, View>();
	
	public interface MyClickListener{
		public boolean clickListener(View v,Integer tag);
	}
	
	public OWinfoAdapter(Context context,List<OverWeightInfo> overWeightInfos, int resource,MyClickListener mListener) {
		this.overWeightInfos = overWeightInfos;
		this.resource = resource;
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mcheck = new ArrayList<Boolean>();
		for(int i=0;i<overWeightInfos.size();i++){
			mcheck.add(false);
		}
		this.mListener = mListener;
	}

	@Override
	public int getCount() {
		return overWeightInfos.size();
	}

	@Override
	public Object getItem(int arg0) {
		return overWeightInfos.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View con, ViewGroup parent) {
		ViewHolder holder = null; // 自定义结构体
		if (con == null) {
			con = inflater.inflate(resource, null);
			holder = new ViewHolder();
			holder.check = (CheckBox) con.findViewById(R.id.check);
			holder.enter_op = (Button) con.findViewById(R.id.enter_op);
			holder.owinfo_shunNumber = (TextView)con.findViewById(R.id.owinfo_shunNumber);
			holder.owinfo_trainNumber = (TextView)con.findViewById(R.id.owinfo_trainNumber);
			holder.owinfo_train = (TextView)con.findViewById(R.id.owinfo_train);
			holder.owinfo_pingming = (TextView)con.findViewById(R.id.owinfo_pingming);
			holder.owinfo_daozhan = (TextView)con.findViewById(R.id.owinfo_daozhan);
			holder.owinfo_fahuoren = (TextView)con.findViewById(R.id.owinfo_fahuoren);
			holder.owinfo_chaoqian = (TextView)con.findViewById(R.id.owinfo_chaoqian);
			holder.owinfo_chuangming = (TextView)con.findViewById(R.id.owinfo_chuangming);
			holder.owinfo_pianzhong = (TextView)con.findViewById(R.id.owinfo_pianzhong);
			holder.owinfo_pianzai = (TextView)con.findViewById(R.id.owinfo_pianzai);
			holder.owinfo_biaozcq = (TextView)con.findViewById(R.id.owinfo_biaozcq);
			con.setTag(holder);
		} else {
			holder = (ViewHolder) con.getTag();
		}
		OverWeightInfo overWeightInfo = overWeightInfos.get(position);
		holder.owinfo_shunNumber.setText("顺号:"+overWeightInfo.getShunNumber());
		holder.owinfo_trainNumber.setText("车号:"+overWeightInfo.getTrainNumber());
		holder.owinfo_train.setText("车种："+overWeightInfo.getTrain());
		holder.owinfo_pingming.setText("品名："+overWeightInfo.getPingming());
		holder.owinfo_daozhan.setText("到站："+overWeightInfo.getDaozhan());
		holder.owinfo_fahuoren.setText("发货人："+overWeightInfo.getFahuoren());
		holder.owinfo_chaoqian.setText("超欠："+overWeightInfo.getChaoqian());
		holder.owinfo_chuangming.setText("船名："+overWeightInfo.getChuangming());
		holder.owinfo_pianzhong.setText("偏重："+overWeightInfo.getPianzhong());
		holder.owinfo_pianzai.setText("偏载："+overWeightInfo.getPianzai());
		holder.owinfo_biaozcq.setText("标超："+overWeightInfo.getBiaozcq());
		holder.check.setChecked(mcheck.get(position));
		holder.check.setTag(position);
		holder.check.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Integer tag = (Integer)v.getTag();
				if(mcheck.get(tag)){
					mcheck.set(tag, false);
				} else {
					mcheck.set(tag, true);
				}
				notifyDataSetChanged();
			}
		});
		holder.enter_op.setTag(position);
		holder.enter_op.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Integer tag = (Integer) v.getTag();
				mListener.clickListener(v,tag);
			}
		});
		return con;
	}
	
	class ViewHolder {
		public CheckBox check;
		public Button enter_op;
		public TextView owinfo_shunNumber;
		public TextView owinfo_trainNumber;
		public TextView owinfo_train;
		public TextView owinfo_pingming;
		public TextView owinfo_daozhan;
		public TextView owinfo_fahuoren;
		public TextView owinfo_chaoqian;
		public TextView owinfo_chuangming;
		public TextView owinfo_pianzhong;
		public TextView owinfo_pianzai;
		public TextView owinfo_biaozcq;
	}
}
